#apt install -y libssl-dev rapidjson-dev libboost-all-dev libre2-dev libb64-dev libnuma-dev patchelf libarchive-dev zlib1g-dev
#pip3 install distro requests build
#pip3 install nvidia-pyindex tritonclient[all]


export TRITON_CONTAINER_VERSION=22.12
export TRITON_REPO_ORGANIZATION=https://gitee.com/121786404

export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/ixdcgm/include
export LIBRARY_PATH=/usr/local/ixdcgm/lib64:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/ixdcgm/lib64:/usr/local/corex/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib/python3.12/dist-packages/tvm:$LD_LIBRARY_PATH
#export NVINFER_LIBRARY
#export NVINFER_PLUGIN_LIBRARY

git config --global --add safe.directory '*'

pushd server
git checkout -f r${TRITON_CONTAINER_VERSION}
./build.py -v --no-container-build \
--build-dir=`pwd`/build \
--install-dir=/opt/tritonserver \
--enable-logging \
--enable-stats \
--enable-metrics \
--enable-cpu-metrics \
--enable-gpu-metrics \
--enable-tracing \
--enable-gpu \
--endpoint=http \
--repo-tag=common:r${TRITON_CONTAINER_VERSION} \
--repo-tag=core:r${TRITON_CONTAINER_VERSION} \
--repo-tag=backend:r${TRITON_CONTAINER_VERSION} \
--repo-tag=thirdparty:r${TRITON_CONTAINER_VERSION} \
--backend=ensemble:r${TRITON_CONTAINER_VERSION} \
--repoagent=checksum:r${TRITON_CONTAINER_VERSION} \
--upstream-container-version=${TRITON_CONTAINER_VERSION} \
--github-organization=${TRITON_REPO_ORGANIZATION} \
-j 64
popd


pushd tis-ixrt-backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_TENSORRT_INCLUDE_PATHS=/usr/local/corex/include \
  -DTRITON_TENSORRT_LIB_PATHS=/usr/local/corex/lib64 \
  -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -DIXRT_HOME=/usr/local/corex \
  ..
make -j
mkdir -p /opt/tritonserver/backends/ixrt
cp libtriton_ixrt.so /opt/tritonserver/backends/ixrt
cd ..
popd


pushd tis-igie-backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -DIGIE_HOME=/usr/local/lib/python3.12/dist-packages/tvm \
  ..
make -j
mkdir -p /opt/tritonserver/backends/igie
cp libtriton_igie.so /opt/tritonserver/backends/igie
cd ..
popd



pushd onnxruntime_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_ONNXRUNTIME_INCLUDE_PATHS=/usr/local/corex/include/onnxruntime \
  -DTRITON_ONNXRUNTIME_LIB_PATHS=/usr/local/corex/lib64 \
  -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -DTRITON_ONNXRUNTIME_DOCKER_BUILD=OFF \
  ..
make -j
mkdir -p /opt/tritonserver/backends/onnxruntime
cp libtriton_onnxruntime.so /opt/tritonserver/backends/onnxruntime
cd ..
popd


pushd dali_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_DALI_SKIP_DOWNLOAD=ON \
  -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
  -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
  -D__ILUVATAR_WORKAROUND__=1 \
  ..
make -j
mkdir -p /opt/tritonserver/backends/dali
cp libtriton_dali.so /opt/tritonserver/backends/dali
cd ..
popd


pushd python_backend
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_BACKEND_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_REPO_ORGANIZATION=${TRITON_REPO_ORGANIZATION} \
      ..
make -j
mkdir -p /opt/tritonserver/backends/python
cp libtriton_python.so triton_python_backend_stub /opt/tritonserver/backends/python
cp ../src/resources/triton_python_backend_utils.py /opt/tritonserver/backends/python
cd ..
popd


pushd developer_tools/server
git checkout -f r${TRITON_CONTAINER_VERSION}
mkdir build
cd build
cmake -DTRITON_CORE_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_COMMON_REPO_TAG=r${TRITON_CONTAINER_VERSION} \
      -DTRITON_BUILD_TEST=OFF \
      -DTRITON_ENABLE_EXAMPLES=OFF ..
make -j
cp libtritondevelopertoolsserver.a /opt/tritonserver/lib/
cp -r ../include/triton/developer_tools /opt/tritonserver/include/triton/
cd ..
popd
